var logger = require("pomelo-logger").getLogger(__filename);

module.exports = function (app) {
    return new Remote(app);
};

var Remote = function (app) {
    this.app = app;
};

Remote.prototype.getSwiperItemById = function (msg, callback) {
    let swiperItemService = this.app.get("swiperItemService");
    if(!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }
    let swiperItem = swiperItemService.getSwiperItemById(parseInt(msg["id"]));
    if(!!swiperItem) {
        callback({
            resultCode: 0,
            message: "ok",
            swiperItem: swiperItem.toJSON()
        });
    }else{
        callback({
            resultCode: 404,
            message: "swiperItem为空.get"
        });
    }

};

Remote.prototype.getAllSwiperItems = function (msg, callback) {
    let swiperItemService = this.app.get("swiperItemService");
    callback({
        resultCode: 0,
        message: "ok",
        swiperItem: swiperItemService.toJSON()
    });
};

Remote.prototype.updateSwiperItemUrl = function (msg, callback) {
    let swiperItemService = this.app.get("swiperItemService");
    if(!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }
    if(!msg.hasOwnProperty("url")) {
        callback({resultCode: 404, message: "url为空"});
        return;
    }
    swiperItemService.updateSwiperItemUrl(parseInt(msg["id"]), msg["url"], function (swiperItem) {
        if(!!swiperItem) {
            callback({
                resultCode: 0,
                message: "ok",
                swiperItem: swiperItem.toJSON()
            });
        }else{
            callback({
                resultCode: 404,
                message: "swiperItem为空.update"
            });
        }
    });
};

Remote.prototype.updateSwiperItemPriority = function (msg, callback) {
    let swiperItemService = this.app.get("swiperItemService");
    if(!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }
    if(!msg.hasOwnProperty("priority")) {
        callback({resultCode: 404, message: "url为空"});
        return;
    }
    swiperItemService.updateSwiperItemPriority(parseInt(msg["id"]), parseInt(msg["priority"]), function (swiperItem) {
        if(!!swiperItem) {
            callback({
                resultCode: 0,
                message: "ok",
                swiperItem: swiperItem.toJSON()
            });
        }else{
            callback({
                resultCode: 404,
                message: "swiperItem为空.update"
            });
        }
    });
};

Remote.prototype.updateSwiperItem = function (msg, callback) {
    let swiperItemService = this.app.get("swiperItemService");
    if(!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }
    if(!msg.hasOwnProperty("priority")) {
        callback({resultCode: 404, message: "priority为空"});
        return;
    }
    if(!msg.hasOwnProperty("url")) {
        callback({resultCode: 404, message: "url为空"});
        return;
    }
    if(!msg.hasOwnProperty("startTime")) {
        callback({resultCode: 404, message: "startTime为空"});
        return;
    }
    if(!msg.hasOwnProperty("endTime")) {
        callback({resultCode: 404, message: "endTime为空"});
        return;
    }

    let id = parseInt(msg["id"]);
    let priority = parseInt(msg["priority"]);
    let url = msg["url"];
    let activityId = 0;
    let startTime = parseInt(msg["startTime"]);
    let endTime = parseInt(msg["endTime"]);

    if(msg.hasOwnProperty("activityId")) {
        activityId = parseInt(msg.activityId);
    }
    swiperItemService.updateSwiperItem(id, priority, url, activityId, startTime, endTime, function (swiperItem) {
        if(!!swiperItem) {
            callback({
                resultCode: 0,
                message: "ok",
                swiperItem: swiperItem.toJSON()
            });
        }else{
            callback({
                resultCode: 404,
                message: "swiperItem为空.update"
            });
        }
    });
};

Remote.prototype.addSwiperItem = function (msg, callback) {
    let swiperItemService = this.app.get("swiperItemService");
    if(!msg.hasOwnProperty("priority")) {
        callback({resultCode: 404, message: "priority为空"});
        return;
    }
    if(!msg.hasOwnProperty("url")) {
        callback({resultCode: 404, message: "url为空"});
        return;
    }
    if(!msg.hasOwnProperty("startTime")) {
        callback({resultCode: 404, message: "startTime为空"});
        return;
    }
    if(!msg.hasOwnProperty("endTime")) {
        callback({resultCode: 404, message: "endTime为空"});
        return;
    }
    let priority = parseInt(msg["priority"]);
    let url = msg["url"];
    let activityId = 0;
    let startTime = parseInt(msg["startTime"]);
    let endTime = parseInt(msg["endTime"]);
    if(msg.hasOwnProperty("activityId")) {
        activityId = parseInt(msg.activityId);
    }
    swiperItemService.addSwiperItem(priority, url, activityId, startTime, endTime, function (swiperItem) {
        if(!!swiperItem) {
            callback({
                resultCode: 0,
                message: "ok",
                swiperItem : swiperItem.toJSON()
            });
        }else{
            callback({
                resultCode: 404,
                message: "swiperItem为空.add"
            });
        }
    });
};

Remote.prototype.deleteSwiperItemById = function (msg, callback) {
    let swiperItemService = this.app.get("swiperItemService");
    if(!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }
    swiperItemService.deleteSwiperItemById(parseInt(msg["id"]), function (id) {
        if(!!id) {
            callback({
                resultCode: 0,
                message: "ok",
                swiperItem : {id : id}
            });
        }else{
            callback({
                resultCode: 0,
                message: "swiperItem为空.delete"
            });
        }
    });
};

